<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-flex1 />
			<!-- 条件搜索 -->
			<cl-search ref="Search" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" setup>
defineOptions({
	name: "appoint-prescription",
});

import { useCrud, useTable, useUpsert, useSearch } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { useI18n } from "vue-i18n";
import { reactive } from "vue";

const { service } = useCool();
const { t } = useI18n();

// 选项
const options = reactive({
	patientGender: [
		{ label: t("未知"), value: 0 },
		{ label: t("男"), value: 1 },
		{ label: t("女"), value: 2 },
	],
	prescriptionStatus: [
		{ label: t("待审核"), value: 0 },
		{ label: t("已审核"), value: 1 },
		{ label: t("已完成"), value: 2 },
		{ label: t("已取消"), value: 3 },
	],
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: t("处方编号"),
			prop: "prescriptionNo",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
			required: true,
		},
		{
			label: t("患者姓名"),
			prop: "patientName",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
			required: true,
		},
		{
			label: t("患者性别"),
			prop: "patientGender",
			component: {
				name: "el-radio-group",
				options: options.patientGender,
			},
			value: 0,
			required: true,
		},
		{
			label: t("患者年龄"),
			prop: "patientAge",
			hook: "number",
			component: { name: "el-input-number", props: { min: 0 } },
			span: 12,
			required: true,
		},
		{
			label: t("诊断结果"),
			prop: "diagnosisResult",
			component: {
				name: "el-input",
				props: { type: "textarea", rows: 4 },
			},
		},
		{
			label: t("医生姓名"),
			prop: "doctorName",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
			required: true,
		},
		{
			label: t("医生科室"),
			prop: "doctorDepartment",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("处方日期"),
			prop: "prescriptionDate",
			component: {
				name: "el-date-picker",
				props: { type: "date", valueFormat: "YYYY-MM-DD" },
			},
			span: 12,
			required: true,
		},
		{
			label: t("处方状态"),
			prop: "prescriptionStatus",
			component: {
				name: "el-radio-group",
				options: options.prescriptionStatus,
			},
			value: 0,
			required: true,
		},
		{
			label: t("备注"),
			prop: "remark",
			component: {
				name: "el-input",
				props: { type: "textarea", rows: 4 },
			},
		},
	],
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: t("姓名"), prop: "sysDoctorName", minWidth: 140 },
		{ label: t("处方编号"), prop: "prescriptionNo", minWidth: 140 },
		{ label: t("患者姓名"), prop: "patientName", minWidth: 140 },
		{
			label: t("患者性别"),
			prop: "patientGender",
			minWidth: 120,
			dict: options.patientGender,
		},
		{
			label: t("患者年龄"),
			prop: "patientAge",
			minWidth: 140,
			sortable: "custom",
		},
		{
			label: t("诊断结果"),
			prop: "diagnosisResult",
			showOverflowTooltip: true,
			minWidth: 200,
		},
		{ label: t("医生姓名"), prop: "doctorName", minWidth: 140 },
		{ label: t("医生科室"), prop: "doctorDepartment", minWidth: 140 },
		{
			label: t("处方日期"),
			prop: "prescriptionDate",
			minWidth: 140,
			sortable: "custom",
			component: {
				name: "cl-date-text",
				props: { format: "YYYY-MM-DD" },
			},
		},
		{
			label: t("处方状态"),
			prop: "prescriptionStatus",
			minWidth: 120,
			dict: options.prescriptionStatus,
		},
		{
			label: t("备注"),
			prop: "remark",
			showOverflowTooltip: true,
			minWidth: 200,
		},
		{
			label: t("创建时间"),
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" },
		},
		{
			label: t("更新时间"),
			prop: "updateTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" },
		},
		{ type: "op", buttons: ["edit", "delete"] },
	],
});

// cl-search
const Search = useSearch();

// cl-crud
const Crud = useCrud(
	{
		service: service.appoint.prescription,
	},
	(app) => {
		app.refresh();
	},
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
